package bt.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import bt.excepcion.DAOExcepcion;
import bt.modelo.Pais;
import bt.util.ConexionBD;

public class PaisDAO extends BaseDAO {
	
	public Collection<Pais> listar() throws DAOExcepcion {
		Collection<Pais> lista = new ArrayList<Pais>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			String query = "SELECT idPais,nombre FROM Pais";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {

				Pais bt = new Pais();
				bt.setIdPais(rs.getInt("idPais"));
				bt.setNombre(rs.getString("nombre"));

				lista.add(bt);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return lista;
	}


}
